package service.impl;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import pojo.User;
import protocal.DataPackage;
import protocal.DataPackage.MessageType;
import protocal.DataPackageFilter;
import protocal.Filter;
import service.Service;

import com.fasterxml.jackson.databind.ObjectMapper;

import dao.UserDao;
import dao.impl.UserDaoImpl;

public class RegisterServiceImpl extends Thread implements Service{
	
	private final static Logger logger = LogManager.getLogger(ChatServiceImpl.class);
	
	private Socket socket;
	private BufferedReader reader;
	private BufferedWriter writer;
	
	
	public RegisterServiceImpl(Socket socket) {
		super();
		this.socket = socket;
		try {
			reader = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
			writer = new BufferedWriter(new OutputStreamWriter(this.socket.getOutputStream()));
		} catch (IOException e) {
			logger.error(e);
		}
	}
	
	@Override
	public void run() {
		service();
	}
	
	@Override
	public void close(){
		try {
			Thread.sleep(2000);
			reader.close();
			writer.close();
			socket.close();
		} catch (IOException e) {
			logger.error(e);
		} catch (InterruptedException e) {
			logger.error(e);
		}
	}

	@Override
	public void service() {
		try {
			ObjectMapper mapper = new ObjectMapper();
			Filter filter = new DataPackageFilter();
			String data = reader.readLine();
			DataPackage dataPackage = mapper.readValue(data, DataPackage.class);
			String name = dataPackage.getFromeName();
			DataPackage res = new DataPackage("\\",name,MessageType.REGISTER,null,System.currentTimeMillis());
			if (name == null||name.equals("")) {
				res.setMessageData("fail:name can not be empty");
				writer.write(mapper.writeValueAsString(res)+"\n");
				writer.flush();
				close();
				return;
			}
			if (dataPackage.getMessageType()!=MessageType.REGISTER) {
				res.setMessageData("fail:name is exist");
				writer.write(mapper.writeValueAsString(res)+"\n");
				writer.flush();
				close();
				return;
			}
			UserDao userDao = new UserDaoImpl();
			User user = userDao.findByName(name);
			if (user!=null) {
				res.setMessageData("fail:name is exist");
				writer.write(mapper.writeValueAsString(res)+"\n");
				writer.flush();
				close();
				return;
			}
			User newUser = new User(null,name,dataPackage.getMessageData());
			int m = userDao.addUser(newUser);
			if (m!=1) {
				res.setMessageData("fail:insert fail");
				writer.write(mapper.writeValueAsString(res)+"\n");
				writer.flush();
				close();
				return;
			}		
			res.setMessageData("success");
			writer.write(mapper.writeValueAsString(res)+"\n");
			writer.flush();
		} catch (IOException e) {
			logger.error(e);
		}finally{
			close();
		}
		
	}


		
}
